#include "Fact.h"

namespace IAlib

{

Fact::Fact()
{
    m_Name = "";
    m_State = false;
    m_type = 0;      // 0 for agent knowledge 1 for environmetal knowledge
}

Fact::Fact( const QString& S )
{
    m_Name = S;
    m_State = false;
    m_type = 0;
}

Fact::Fact( const QString& S, const bool& state )
{
    m_Name = S;
    m_State = state;
    m_type = 0;
}


Fact::Fact( const Fact & fact )
{
    m_Name = fact.m_Name;
    m_State = fact.m_State;
    m_type = fact.m_type;
    m_pos = fact.m_pos;
}

//////////////////////////////////////////////////////////////////////

QDomElement Fact::toXML(QDomDocument &doc)
{
    QDomElement ele = doc.createElement("Fact");
    QString  s;
    if (m_State == true)
    {
        s = "true";
    }
    else
    {
        s = "false";
    }
    ele.setAttribute("FactState", s);
    ele.setAttribute("FactName", m_Name);
    ele.setAttribute("Type", m_type);
    if (m_type ==1)
    {
        ele.appendChild(m_pos.toXML(doc));
    }
    return ele;
}

void Fact::fromXML(QDomElement element)
{
    m_Name = element.attribute("FactName","");
    if (element.attribute("FactState","") == "true")
    {
        m_State = true;
    }
    else
    {
        m_State = false;
    }
    m_type = element.attribute("Type","").toInt();
    if (m_type == 1)
    {
        QDomNode position = element.firstChild();
        m_pos.fromXML(position.toElement());
    }
    else
    {
        m_pos.m_x = 0;
        m_pos.m_y = 0;
        m_pos.m_z = 0;
    }
}

//------------------

QByteArray Fact::toByteArray()
{
	QByteArray byte_array;
	QDataStream data(&byte_array, QIODevice::WriteOnly);
	data<<m_Name;
	data<<m_State;
	return byte_array;
}

//----------------------------

void Fact::fromByteArray(QByteArray& byte_array)
{
	QDataStream data(&byte_array, QIODevice::ReadOnly);
	data>>m_Name;
	data>>m_State;
}

////////////////////////////////////////////////////////////////////////

QString Fact::getFactName() const
{
    return m_Name;
}
//---------------------------------------
bool Fact::getFactStat() const
{
    return m_State;
}
//---------------------------------------
Position Fact::getPosition() const
{
    return m_pos;
}
//---------------------------------------
unsigned char Fact::getType() const
{
    return m_type;
}
//---------------------------------------
void Fact::setFactStat(const bool state)
{
    m_State = state;
}
//----------------------------------------
void Fact::setFactName(const QString& S)
{
    m_Name = S;
}
//----------------------------------------
void Fact::setFactType(const unsigned char type)
{
    m_type = type;
}
//----------------------------------------
void Fact::setPosition(const Position& pos)
{
    m_pos = pos;
}

/////////////////////////////////////////////////////////////////

}

